<template>
    <van-field
            v-model="data.value"
            :type="data.type"
            :required="data.required === 1"
            :placeholder="data.placeHolder"
            :name="data.fieldName"
            :label="data.label"
            :rules="rules"
            :show-word-limit="data.type === 'textarea' && data.maxLength > 0"
            :maxlength="parseInt(data.maxLength) === 0 ? Infinity : data.maxLength"
    >
    </van-field>
</template>

<script>
    import validateRules from './validates'
    export default {
        props: {
            data: Object
        },
        computed:{
            rules(){
                let rules = [];
                if (this.data.required === 1) {
                    rules.push({
                        required: true,
                        message : this.data.emptyTips
                    })
                }
                if (this.data.validateCode !== null && this.data.validateCode !== ''){
                    let code = this.data.validateCode ;
                    validateRules.forEach(function (rule) {
                        if (code === rule.code){
                            let pattern = rule.reg;
                            rules.push({
                                pattern,
                                message : rule.tips !== undefined ? rule.tips : (rule.name + '格式不正确')
                            })
                        }
                    });
                }
                return rules
            }
        },
    }
</script>